package com.wngbms.simple;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
public class UDPClient {

    public static void main(String[] args) throws IOException, InterruptedException {
        InetAddress address = InetAddress.getByName("192.168.31.79");
        int port = 8888;

        byte[] bytes = "client hello".getBytes();
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);

        byte[] data = new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(data, data.length);
        socket.receive(packet1);

        Thread.sleep(5000);

        String resp = new String(data, 0, packet1.getLength());
        log.info("client recv:{}", resp);
        socket.close();
    }

}
